<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                <FormItem :label="t('xPotfp.TKID')" type="string" v-model="baTable.form.items!.TKID" prop="TKID" :input-attr="{ placeholder: t('Please input field', { field: t('xPotfp.TKID') }) }" />
                <FormItem :label="t('xPotfp.TCNO')" type="string" v-model="baTable.form.items!.TCNO" prop="TCNO" :input-attr="{ placeholder: t('Please input field', { field: t('xPotfp.TCNO') }) }" />
                <FormItem :label="t('xPotfp.YARD')" type="string" v-model="baTable.form.items!.YARD" prop="YARD" :input-attr="{ placeholder: t('Please input field', { field: t('xPotfp.YARD') }) }" />
                <FormItem :label="t('xPotfp.KTXTIME')" type="datetime" v-model="baTable.form.items!.KTXTIME" prop="KTXTIME" :input-attr="{ placeholder: t('Please select field', { field: t('xPotfp.KTXTIME') }) }" />
                <FormItem :label="t('xPotfp.STARTDATE')" type="datetime" v-model="baTable.form.items!.STARTDATE" prop="STARTDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xPotfp.STARTDATE') }) }" />
                <FormItem :label="t('xPotfp.ENDDATE')" type="datetime" v-model="baTable.form.items!.ENDDATE" prop="ENDDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xPotfp.ENDDATE') }) }" />
                <FormItem :label="t('xPotfp.YJENDDATE')" type="datetime" v-model="baTable.form.items!.YJENDDATE" prop="YJENDDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xPotfp.YJENDDATE') }) }" />
                <FormItem :label="t('xPotfp.LOCKED')" type="string" v-model="baTable.form.items!.LOCKED" prop="LOCKED" :input-attr="{ placeholder: t('Please input field', { field: t('xPotfp.LOCKED') }) }" />
                <FormItem :label="t('xPotfp.LOCKDATE')" type="datetime" v-model="baTable.form.items!.LOCKDATE" prop="LOCKDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xPotfp.LOCKDATE') }) }" />
                <FormItem :label="t('xPotfp.REMARK')" type="string" v-model="baTable.form.items!.REMARK" prop="REMARK" :input-attr="{ placeholder: t('Please input field', { field: t('xPotfp.REMARK') }) }" />
                <FormItem :label="t('xPotfp.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: t('Please input field', { field: t('xPotfp.OP') }) }" />
                <FormItem :label="t('xPotfp.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: t('Please select field', { field: t('xPotfp.OPTIME') }) }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    KTXTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.KTXTIME') })})],
    STARTDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.STARTDATE') })})],
    ENDDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.ENDDATE') })})],
    YJENDDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.YJENDDATE') })})],
    LOCKDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.LOCKDATE') })})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.OPTIME') })})],
})

</script>

<style scoped lang="scss"></style>
